package com.ruoyi.know.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.know.domain.UserLikes;
import com.ruoyi.know.service.IPointRecordsService;
import com.ruoyi.know.service.IUserLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 用户点赞Controller
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@RestController
@RequestMapping("/know/likes")
public class UserLikesController extends BaseController
{
    @Autowired
    private IUserLikesService userLikesService;
    @Autowired
    private IPointRecordsService pointRecordsService;

    /**
     * 查询用户点赞列表
     */
    @PreAuthorize("@ss.hasPermi('know:likes:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserLikes userLikes)
    {
        startPage();
        List<UserLikes> list = userLikesService.selectUserLikesList(userLikes);
        return getDataTable(list);
    }

    /**
     * 导出用户点赞列表
     */
    @PreAuthorize("@ss.hasPermi('know:likes:export')")
    @Log(title = "用户点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserLikes userLikes)
    {
        List<UserLikes> list = userLikesService.selectUserLikesList(userLikes);
        ExcelUtil<UserLikes> util = new ExcelUtil<UserLikes>(UserLikes.class);
        util.exportExcel(response, list, "用户点赞数据");
    }

    /**
     * 获取用户点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('know:likes:query')")
    @GetMapping(value = "/{likeId}")
    public AjaxResult getInfo(@PathVariable("likeId") Long likeId)
    {
        return success(userLikesService.selectUserLikesByLikeId(likeId));
    }

    /**
     * 新增用户点赞
     */
    @PreAuthorize("@ss.hasPermi('know:likes:add')")
    @Log(title = "用户点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserLikes userLikes)
    {
        userLikes.setCreateAt(new Date());
        //  如果未传用户id，则默认为当前用户 待定
//        if(userLikes.getUserId() == null){
//            Long userId = getUserId();
//            userLikes.setUserId(userId);
//        }
        //加积分
        pointRecordsService.earnPoints(getUserId(),"点赞",true);
        return toAjax(userLikesService.insertUserLikes(userLikes));
    }

    /**
     * 修改用户点赞
     */
    @PreAuthorize("@ss.hasPermi('know:likes:edit')")
    @Log(title = "用户点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserLikes userLikes)
    {
        return toAjax(userLikesService.updateUserLikes(userLikes));
    }

    /**
     * 删除用户点赞
     */
    @PreAuthorize("@ss.hasPermi('know:likes:remove')")
    @Log(title = "用户点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{likeIds}")
    public AjaxResult remove(@PathVariable Long[] likeIds)
    {
        return toAjax(userLikesService.deleteUserLikesByLikeIds(likeIds));
    }

    /**
     * 取消点赞
     */
    @PreAuthorize("@ss.hasPermi('know:likes:clickLike')")
    @Log(title = "取消点赞", businessType = BusinessType.DELETE)
    @PostMapping("/clickLike")
    public AjaxResult clickLike(@RequestBody UserLikes userLikes)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        userLikes.setUserId(loginUser.getUserId());
        //减积分
        pointRecordsService.earnPoints(getUserId(),"点赞",false);
        return toAjax(userLikesService.deleteUserLikesByUserLikes(userLikes));
    }
}
